﻿using System;
using System.Threading;

namespace ThreadingStudy
{
    class Program
    {
        static void Main(string[] args)
        {

            TestThread_5();
        }

        static void TestThread_1()
        {
            Thread t = new Thread(theadMethod);
            t.Start();
            t.Name = "另一个线程";
            Console.WriteLine(t.IsAlive);
            for (int i = 0; i < 1000; i++)
            {
                Console.Write("y");
                Console.WriteLine(Thread.CurrentThread.Name);
            }
            Console.WriteLine(t.IsAlive);
        }

        static void theadMethod()
        {
            for (int i = 0; i < 1000; i++)
            {
                Console.Write("x");
                Console.WriteLine(Thread.CurrentThread.Name);
            }
        }

        static void TestThread_2()
        {
            Thread t = new Thread(Go);
            t.Start();
            t.Join();
            Console.WriteLine("Thread t has ended!");
        }

        static void Go()
        {
            for (int i = 0; i < 20000; i++)
            {
                Thread.Yield();
                Console.Write("Joey_");
            }
        }

        static void TestThread_3()
        {
            Thread t = new Thread(() => Print("GO,GO,GO"));
            t.Start();
        }

        static void Print(string Message)
        {
            Console.WriteLine(Message);
        }

        static void TestThread_4(string[] args)
        {
            Thread worker = new Thread(() => Console.ReadLine());
            if (args.Length > 0)
                worker.IsBackground = true;
            worker.Start();
        }


        static void TestThread_5()
        {
            var signal = new ManualResetEvent(false);
            new Thread(() =>
            {
                Console.WriteLine("等待信号...");
                signal.WaitOne();
                //signal.Dispose();
                signal.Reset();
                Console.WriteLine("获得信号");
            }).Start();

            Thread.Sleep(2000);
            signal.Set();
        }
    }
}
